/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ez2find.manager;

import com.ez2find.model.Topic;
import com.ez2find.model.Forum;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author org.@le
 */
@Stateless
public class TopicManagerBean implements TopicManagerLocal {

    @PersistenceContext
    private EntityManager em;

    public void create(Topic topic) throws Exception {
        em.persist(topic);
    }

    public void update(Topic topic) throws Exception {
        em.merge(topic);
    }

    public void delete(Topic topic) throws Exception {
        em.remove(topic);
    }

    public Topic find(long id) {
        return em.find(Topic.class, id);
    }

    public List<Topic> listAll() {
        return em.createQuery("select c from Topic c").getResultList();

    }

    public List<Topic> topicsForum(Forum forum) {
        Query query = em.createNamedQuery("topicsForum");
        query.setParameter("forum", forum);
        List<Topic> result = query.getResultList();
        return result;
    }
    
    public List<Topic> invertedTopics(Forum forum) {
        Query query = em.createNamedQuery("topicsForumInv");
        query.setParameter("forum", forum);
        List<Topic> result = query.getResultList();
        return result;
    }
}
